package java44th.group.service.admin.region;

import java44th.group.bean.po.MarketRegion;
import java44th.group.bean.po.MarketRegionExample;
import java44th.group.bean.admin.vo.region.RegionVo;
import java44th.group.mapper.MarketRegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Aurhor: 11437
 * @DATE: 2022/10/11 19:56
 * @Description:
 * @Version 1.0
 */
@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    MarketRegionMapper regionMapper;

    @Override
    @Transactional
    public RegionVo regionList() {

        MarketRegionExample marketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria criteria = marketRegionExample.createCriteria();
        byte provinceType = 1;
        criteria.andTypeEqualTo(provinceType);
        //先查询省的数目
        long count = regionMapper.countByExample(marketRegionExample);
        RegionVo regionVo = new RegionVo();
        //将省的数量,limit,page,pages返回,只有一页
        regionVo.setLimit((int) count);
        regionVo.setPage(1);
        regionVo.setPages(1);
        regionVo.setTotal((int) count);
        //查询RegionVo的成员变量List


        List<MarketRegion> marketRegions = regionMapper.selectByExample(marketRegionExample);
        //循环将listDTO赋值,除了list
        List<RegionVo.ListDTO> listDTOS = new ArrayList<>();

        //这是查省的信息
        for (MarketRegion marketRegion : marketRegions) {
            RegionVo.ListDTO listDTO = new RegionVo.ListDTO(marketRegion.getId(), marketRegion.getName(),
                    marketRegion.getType(), marketRegion.getCode());
            //    将RegionVo.ListDTO的list赋值,这是查市的信息
            MarketRegionExample marketRegionExampleCity = new MarketRegionExample();
            MarketRegionExample.Criteria criteriaCity = marketRegionExampleCity.createCriteria();

            criteriaCity.andPidEqualTo(listDTO.getId());

            List<MarketRegion> marketRegionsCity = regionMapper.selectByExample(marketRegionExampleCity);
            //    循环将城市的信息传回


            List<RegionVo.ListDTO.ChildrenDTO1> childrenDTO1s =  new ArrayList<>();;
            for (MarketRegion region : marketRegionsCity) {


                RegionVo.ListDTO.ChildrenDTO1 childrenDTO1 = new RegionVo.ListDTO.ChildrenDTO1(region.getId(), region.getName(), region.getType(), region.getCode());
                //查询区的信息
                MarketRegionExample marketRegionExampleArea = new MarketRegionExample();
                MarketRegionExample.Criteria criteriaArea = marketRegionExampleArea.createCriteria();
                criteriaArea.andPidEqualTo(childrenDTO1.getId());
                List<MarketRegion> marketRegions1 = regionMapper.selectByExample(marketRegionExampleArea);
                List<RegionVo.ListDTO.ChildrenDTO1.ChildrenDTO2> childrenDTO2s =  new ArrayList<>();

                for (MarketRegion marketRegion1 : marketRegions1) {
                    RegionVo.ListDTO.ChildrenDTO1.ChildrenDTO2 childrenDTO2 = new RegionVo.ListDTO.ChildrenDTO1.ChildrenDTO2(marketRegion1.getId(), marketRegion1.getName(), marketRegion1.getType(), marketRegion1.getCode());

                    childrenDTO2s.add(childrenDTO2);

                }
                childrenDTO1.setChildren(childrenDTO2s);

                childrenDTO1s.add(childrenDTO1);
            }
            listDTO.setChildren(childrenDTO1s);
            listDTOS.add(listDTO);
        }
        regionVo.setList(listDTOS);

        //
        return regionVo;

    }


}
